from django.db import models

from apps.user.models import UserModel


# Create your models here.
class ProjectModel(models.Model):
    class Meta:
        db_table = 'project'
        verbose_name = '项目'
        verbose_name_plural = verbose_name

    name = models.CharField(max_length=20, null=False, verbose_name='项目名')
    description = models.CharField(max_length=512, null=False, default='还什么都没有哦', verbose_name='项目描述')
    create_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    def __str__(self):
        return self.name


class ProjectUserModel(models.Model):
    class Meta:
        db_table = 'project_user'
        verbose_name = '项目用户关系'
        verbose_name_plural = verbose_name

    STATUS_CHOICES = ((0, '默认状态'), (1, '已邀请'), (2, '已加入'), (3, '拒绝加入'), (4, '已退出'))
    RIGHT_CHOICES = ((0, '管理员'), (1, '只读'), (2, '可读可写'))

    project_id = models.ForeignKey(to=ProjectModel, on_delete=models.CASCADE, verbose_name='项目id')
    user_id = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='用户id')
    status = models.IntegerField(choices=STATUS_CHOICES, default=-1)
    create_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间（发送邀请）')
    update_at = models.DateTimeField(auto_now=True, verbose_name='修改时间（同意/拒绝/退出）')
    rights = models.IntegerField(choices=RIGHT_CHOICES, default=1, verbose_name='权限')

    def __str__(self):
        return self.rights


class ProjectLogModel(models.Model):
    class Meta:
        db_table = 'project_log'
        verbose_name = '项目操作记录'
        verbose_name_plural = verbose_name

    project_id = models.ForeignKey(to=ProjectModel, on_delete=models.CASCADE, verbose_name='项目id')
    user_id = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='用户id')
    msg = models.TextField(null=False, verbose_name='日志')
    type = models.CharField(null=False, max_length=128, verbose_name='类型')
    data = models.TextField(null=False, verbose_name='数据')
    create_at = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')

    def __str__(self):
        return self.create_at
